﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWebPagesStarterKit.Controls;

public partial class Administration_Statistics : PageBaseClass
{
     /// <summary>
    /// This method shows you how to generate a Pie Chart using the GoogleChart class.
    /// </summary>
    private void CreatePieChart(string date)
    {
        List<HttpData> data = new List<HttpData>();

        if (CalStat.SelectedDates != null)
        {
            SelectedDatesCollection dates = CalStat.SelectedDates;

            foreach (DateTime dd in dates)
            {
                string day = string.Format("{0}-{1}-{2}", dd.Year, dd.Month, dd.Day);

                string statfile = Server.MapPath("~/App_Data/Stats/" + day + ".txt");
                if (File.Exists(statfile))
                {
                    TextReader reader = File.OpenText(statfile);
                    foreach (string line in reader.ReadToEnd().Split(new char[] { '\n' }))
                    {
                        string[] array = line.Split(new char[] { '|' });
                        string url = array[0];
                        if (!string.IsNullOrEmpty(url))
                        {
                            HttpData found = data.Find(delegate(HttpData d) { return d.Url == url; });
                            if (found != null)
                            {
                                found.Count += 1;
                            }
                            else
                            {
                                data.Add(new HttpData(url, Convert.ToUInt64(1)));
                            }
                        }
                    }
                }
            } 
        }

        if (data.Count > 0)
        {
            string val = string.Empty;
            string page = string.Empty;
            float f = 0;
            foreach (HttpData g in data)
                f += g.Count;

            foreach (HttpData d in data)
            {
                double percent = Convert.ToDouble(d.Count / f) * 100; //PageCount / numberOfPages * 100
                val += Math.Ceiling(percent) + ",";
                page += d.Url + " (" + d.Count + ")|";
            }

            val = val.Substring(0, val.Length - 1);
            page = page.Substring(0, page.Length - 1);

            string chart = "http://chart.apis.google.com/chart?";
            chart += "chs=550x200";
            chart += "&cht=p3";
            chart += "&chd=t:" + val;
            chart += "&chl=" + page;
            pieChart.Visible = true;
            pieChart.ImageUrl = chart;
        }
        else
        {
            pieChart.Visible = false;
        }
    }


    protected void CalStat_SelectionChanged(object sender, EventArgs e)
    {
        string dat = string.Format("{0}-{1}-{2}", CalStat.SelectedDate.Year,CalStat.SelectedDate.Month,CalStat.SelectedDate.Day);
        CreatePieChart(dat);
    }

    protected void CalStat_DayRender(object sender, DayRenderEventArgs e)
    {
        if (!e.Day.IsSelected)
        {
            e.Cell.Controls.Clear();
            e.Cell.Text = e.Day.DayNumberText;
        }
    }

    public class HttpData
    {
        private string _Url;
        private float _Count;

        public HttpData(string url, float count)
        {
            this._Url = url;
            this._Count = count;
        }

        public string Url
        {
            get { return _Url; }
            set { this._Url = value; }
        }

        public float Count
        {
            get { return _Count; }
            set { this._Count = value; }
        }
    }
}
